/**
 * Created by liang.chen on 2014/9/24.
 */
var express = require('express')
    , routes = require('./routes')
    , user = require('./routes/user')
    , http = require('http')
    ,bodyParser = require('body-parser')
    , path = require('path');

var app = express();

var Mongoose = require('mongoose');

var opts = { server: { auto_reconnect: false }, user: 'root', pass: '' };
var db = Mongoose.createConnection('127.0.0.1', 'test',  opts);

var TodoSchema = require('./models/Todo.js').TodoSchema;
var Todo = db.model('todos', TodoSchema);

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

//app.use(express.favicon());
//app.use(express.logger('dev'));
//app.use(express.bodyParser());
app.use(bodyParser());
// development only
if ('development' == app.get('env')) {
//app.use(express.methodOverride());
//app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));

    //app.use(express.errorHandler());
}

app.get('/', routes.index(Todo));
app.get('/users', user.list);

app.get('/todos.json', routes.get(Todo));

app.put('/todo/:id.json', routes.update(Todo));

app.post('/todo.json', routes.addTodo(Todo));


http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});